//
//  cameraViewController.m
//  iSeeColor
//
//  Created by Narbeh Shahnazarian on 10/20/12.
//  Copyright (c) 2012 Wyatt Gosling. All rights reserved.
//

#import "cameraViewController.h"
#import "Daltonizer.h"
#import "Storage.h"
#import "VideoViewController.h"
#import "gpuImageCameraViewController.h"
#import "splitViewVideoViewController.h"

@interface cameraViewController ()

@end

@implementation cameraViewController
//added this
@synthesize popover;

-(IBAction)camera
{
    //instantiate the UIImagePickerController 
    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    
    // Picture Segment
    if (sc.selectedSegmentIndex == 0)
    {
        //check to see if device has a camera
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
        {
            //check if the device is an iPad. If so then create an UIPopOverController to display the choosen image from the users photo library
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            
            //if no camera then default to photo library
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                
            //instantiate the popover with the imagePicker contents
            self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
            
            //set self to the popoverContoller showing the Photo Library
            [self.popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0)
                                     inView:self.view
                   permittedArrowDirections:UIPopoverArrowDirectionAny
                                   animated:YES];
            }//end inner if
            
            //device is an iPhone/iPod then we can proceed with normal UIimagePickerController use
            else
            {
            //if no camera then default to photo library
            [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            
            //set view to present the picked image
            [self presentViewController:picker animated:YES completion:Nil];
            }//end inner else
            
        }//end if for camera check
        
        //device has a camera
        else 
        {
            
            //[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
            
            //create an instance of gpuImageCameraViewController and match it with gpuImageCamera storyboard element.
            gpuImageCameraViewController *camView = [self.storyboard instantiateViewControllerWithIdentifier:@"gpuImageCamera"];
            
             //[self presentViewController:camView animated:YES completion:nil];
            [self.navigationController pushViewController:camView animated:YES];
            sc.selectedSegmentIndex = -1;
            
        }//end else
               
    }//end outer if for segment 0
    
    // Video Segment
    else if(sc.selectedSegmentIndex == 1)
    {
        //instantiate a VideoViewContoller 
        VideoViewController *daltView = [self.storyboard instantiateViewControllerWithIdentifier:@"fullVideoTransform"];
        
        //present the video view 
        //[self presentViewController:daltView animated:YES completion:nil];
        [self.navigationController pushViewController:daltView animated:YES];
        sc.selectedSegmentIndex = -1;
    }//end else for segment 1
    
    //split view was choosen
    else if (sc.selectedSegmentIndex == 2)
    {
        //instantiate  a split view controller
        splitViewVideoViewController *split_view = [self.storyboard instantiateViewControllerWithIdentifier:@"splitView"];
        
        //present the split view with filter on top and normal video on bottom.
        //[self presentViewController:split_view animated:YES completion:nil];
        [self.navigationController pushViewController:split_view animated:YES];
        sc.selectedSegmentIndex = -1;
       
    }//END else if
    
    //switch to profile
    else if(sc.selectedSegmentIndex == 3)
    {
        UIViewController *profile_menu;
        
        //check if the device in question is an iPhone
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {

            profile_menu = [self.storyboard instantiateViewControllerWithIdentifier:@"iPhoneProfile"];
            
        }//end if 
        
        //device is an iPad
        else
        {
        
            profile_menu = [self.storyboard instantiateViewControllerWithIdentifier:@"iPadProfile"];
        
        }//end else
        
        //set the view to appropriate profile view
        [self presentViewController:profile_menu animated:YES completion:nil];
        
    }//end else if
    
    //main menu
    else if(sc.selectedSegmentIndex == 4)
    {
        
        UIViewController *Main;
        
        //check if device in question is an iPhone
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            
           Main = [self.storyboard instantiateViewControllerWithIdentifier:@"iPhoneMain"];
            
        }//end if
        
        //device is an iPad
        else
        {
            
            Main = [self.storyboard instantiateViewControllerWithIdentifier:@"iPadMain"];
            
        }//end else
        
        //set the view to the appropriate main menu
        [self presentViewController:Main animated:YES completion:nil];
    }//end else if

    
}

//This is used only if device does not have a camera
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //grab the photo 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    //run daltonize algorithm
    Daltonizer *dal = [[Daltonizer alloc] init];
    UIImage *dalImage = [dal daltonize:image];
    
    //save daltonized photo to photoLibrary
    [[Storage getInstance] save:dalImage];
    
    //push the image onto the screen
    [imageView setImage:dalImage];
    
    //dismiss the view
    if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)
    {
        
    [self dismissViewControllerAnimated:YES completion:Nil];
        
    }//end if
    
    else
    {
        
    [popover dismissPopoverAnimated:YES];
        
    }//end else
    
}

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:Nil];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //we set sc to one so no button appear pushed at first boot
    sc.selectedSegmentIndex = -1;
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
